//
//  TableImageView.m
//  SZBank
//
//  Created by rimi on 16/7/11.
//  Copyright © 2016年 LuoYao. All rights reserved.
//

#import "TableImageView.h"
#import "BJBackGroundView.h"
@interface TableImageView ()
{
    CGFloat frameWith;/**< 自身宽度 */
    CGFloat frameHeight; /**< 自身高度 */
    NSInteger ItableStyle;/**< 展示页面的style */
    NSMutableArray *compareBlockArray;/**< 对比数据源 */
    NSMutableDictionary *showDatadictionary;/**< 展示数据整理字典 */

    
}
@property (nonatomic)NSDictionary *dataSourceDictionary;/**< 数据源字典 */
@property (nonatomic)UILabel *headTitleLabel;/**< 头部标题 */
@property (nonatomic)UILabel *deadlineLabel;/**< 产品投资期限 */
@property (nonatomic)UILabel *YieldLabel;/**< 产品年化收益率 */
@property (nonatomic)UILabel *originMoney;/**< 起点金额 */
@property (nonatomic)UIImageView *sellBank;/**< 发售银行 */
@property (nonatomic)UILabel *buyOriginDay;/**< 认购起点日 */
@property (nonatomic)UILabel *buyDeadDay;/**< 认购结束日 */
@property (nonatomic)UILabel *originInterest;/**< 起利息日 */
@property (nonatomic)UILabel *deadInterest;/**< 到期日 */
@property (nonatomic)UIButton *incomeCalculation;/**< 收益试算 */
@property (nonatomic)UIButton *addToCompare;/**< 加入对比 */
@end
@implementation TableImageView

- (instancetype)initWithFrame:(CGRect)frame withDataDictionary:(NSDictionary *)dataDictionary tableStyle:(NSInteger)tableStyle
{
    self = [super initWithFrame:frame];
    if (self) {

        //对比数据源赋值
        compareBlockArray = [NSMutableArray array];
        compareBlockArray = dataDictionary[@"labels"];
        //打开可交互属性
        self.userInteractionEnabled = YES;
        //数据整理
        showDatadictionary = [NSMutableDictionary dictionary];
       NSMutableArray *showDataArray_nomal = [NSMutableArray array];
        NSMutableArray *showDataArray_difierent = [NSMutableArray array];
        //第一个元素:
       [showDataArray_nomal addObject:[NSString stringWithFormat:@"【%@】",dataDictionary[@"labels"][0]]];
        //第二个元素
       [showDataArray_nomal addObject:[NSString stringWithFormat:@"%@系列将于<%@>至<%@>在我行各网点及网",dataDictionary[@"labels"][0],dataDictionary[@"labels"][4],dataDictionary[@"labels"][5]]];
        //第三个元素
        [showDataArray_nomal addObject:dataDictionary[@"labels"][0]];
        //后面的元素
        [showDataArray_nomal addObject:dataDictionary[@"labels"][4]];
        [showDataArray_nomal addObject:dataDictionary[@"labels"][5]];
        [showDataArray_nomal addObject:dataDictionary[@"labels"][6]];
        [showDataArray_nomal addObject:dataDictionary[@"labels"][1]];
        
        [showDatadictionary setObject:showDataArray_nomal forKey:@"nomal"];
        if (tableStyle == 0) {
            [showDataArray_difierent addObject:dataDictionary[@"labels"][3]];
            [showDataArray_difierent addObject:dataDictionary[@"labels"][2]];
            [showDatadictionary setObject:showDataArray_difierent forKey:@"diffierent"];
        }else{
           
            if (((NSArray *)(dataDictionary[@"dataSource"][@"dataArray"][@"YIELDDESC"])).count == 4) {
                for (int i = 0; i < 4; i ++) {
                    [showDataArray_difierent addObject:  dataDictionary[@"dataSource"][@"dataArray"][@"YIELDDESC"][i][@"lower"]];
                }
                for (int i = 0; i < 4; i ++) {
                    [showDataArray_difierent addObject:  dataDictionary[@"dataSource"][@"dataArray"][@"YIELDDESC"][i][@"yield"]];
                }
                [showDatadictionary setObject:showDataArray_difierent forKey:@"diffierent"];

            }
        }
        
        
        
        
        //给tableStyle赋值
        ItableStyle = tableStyle;
        self.image = [LocalDataManager getImageWithSourceOfPath:@"理财_底"];
        [self.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        self.dataSourceDictionary = dataDictionary;
        frameWith = self.frame.size.width;
        frameHeight = self.frame.size.height;
        //赋值
        NSArray *labelArray = @[self.headTitleLabel, self.deadlineLabel, self.YieldLabel, self.originMoney, self.buyOriginDay, self.buyDeadDay, self.originInterest, self.deadInterest];
        for (int i = 0; i < 8; i ++) {
            ((UILabel *)labelArray[i]).text = self.dataSourceDictionary[@"labels"][i];
            [self addSubview:((UILabel *)labelArray[i])];
        }
        
        

        
        //添加银行到视图
        [self addSubview:self.sellBank];
        [self addSubview:self.addToCompare];
        [self addSubview:self.incomeCalculation];
       
        
        
        //通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(action_notification) name:@"BJnotification" object:nil];
        
    }
    return self;
}




#pragma mark -- actions
- (void)action_notification{
    for (UIView *view in self.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            if (((UIButton *)view).tag  == 101) {
                ((UIButton *)view).selected = NO;
            }
        }
    }
    
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}



- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //获取触摸点
    CGPoint touchPoint = [[touches anyObject] locationInView:self];
    //确定触摸范围
    CGRect touchRect = CGRectMake(0, 0, frameWith, frameHeight * 0.88);
    if (CGRectContainsPoint(touchRect, touchPoint)) {
        BJBackGroundView *showImage = [[BJBackGroundView alloc]initWithFrame:self.window.frame dataDictionary:showDatadictionary imageStyle:1 tableStyle:ItableStyle];
     
        [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:showImage];
    }
    
}


- (void)action_button:(UIButton *)sender{
    if (sender.tag == 100 ) {
        NSMutableArray *array = [self.dataSourceDictionary[@"labels"] mutableCopy];

        for (int i = 0; i < 5; i ++) {
            [array removeLastObject];
        }
        [array insertObject:@"创盈" atIndex:0];
        BJBackGroundView *backGroundView = [[BJBackGroundView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT) dataDictionary:@{@"style":@"",@"labels":array} imageStyle:calculationStyle tableStyle:0];
        [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:backGroundView];
    }
    if (sender.tag == 101) {
        NSMutableArray *array_compare = [NSMutableArray array];
        NSMutableDictionary *dictionary_compare = [NSMutableDictionary dictionary];
        //将需要的数据假如数组
        for (int i = 0; i < 4; i ++) {
            [array_compare addObject:compareBlockArray[i]];
        }
        //将数据源加入数组
        [dictionary_compare setObject:array_compare forKey:@"dataSource"];
        
        //设置button不同状态背景图
         [sender setBackgroundImage:[LocalDataManager getImageWithSourceOfPath:@"理财_已加入"] forState:UIControlStateSelected];
         [sender setBackgroundImage:[LocalDataManager getImageWithSourceOfPath:@"理财_加入对比"] forState:UIControlStateNormal];
        
        
        
        
        //            改变button状态的时候，记录状态
        if (sender.selected) {
            [dictionary_compare setObject:array_compare forKey:@"dataSource"];
            
            sender.selected = NO;
            
        }else{
            [dictionary_compare setObject:array_compare forKey:@"dataSource"];
            sender.selected = YES;
        }
        
        //block 传值
        if (self.tableViewBlock ) {
            self.tableViewBlock(dictionary_compare);
        }
        
        
   
    }
}



#pragma mark -- getters
/**
 *
 * 数据源
 *  @return
 */
- (NSDictionary *)dataSourceDictionary{
    if (!_dataSourceDictionary) {
        _dataSourceDictionary = [NSDictionary dictionary];
    }
    return _dataSourceDictionary;
}
/**
 *  头部标题
 *
 *  @return
 */
- (UILabel *)headTitleLabel{
    if (!_headTitleLabel) {
        _headTitleLabel = ({
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0,0, frameWith, frameHeight * 0.12)];
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:14.0];
        
            label;
        });
    }
    return _headTitleLabel;
}
/**
 *  投资期限
 *
 *  @return
 */
-(UILabel *)deadlineLabel{
    if (!_deadlineLabel) {
        _deadlineLabel =({
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(frameWith* 0.5,CGRectGetMaxY(self.headTitleLabel.frame) +frameHeight * 0.02,frameWith* 0.5 , frameHeight * 0.07)];
            label.textAlignment = NSTextAlignmentLeft;
            label.textColor = [UIColor colorWithRed:164 / 255.0 green:138 / 255.0 blue:60 / 255.0 alpha:1.000];
            label.font = [UIFont systemFontOfSize:22.0];

            
            label;
        });

    }
    return _deadlineLabel;
}
/**
 *  年化收益
 *
 *  @return
 */
- (UILabel *)YieldLabel{
    if (!_YieldLabel) {
        _YieldLabel = ({
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(frameWith* 0.5,CGRectGetMaxY(self.deadlineLabel.frame)  + frameHeight * 0.01,frameWith* 0.5 , frameHeight * 0.07)];
            label.textAlignment = NSTextAlignmentLeft;
            label.textColor = [UIColor colorWithRed:164 / 255.0 green:138 / 255.0 blue:60 / 255.0 alpha:1.000];
            label.font = [UIFont systemFontOfSize:22.0];
            
            label;
        });

    }
    return _YieldLabel;
}
/**
 *  起点金额
 *
 *  @return
 */
- (UILabel *)originMoney{
    if (!_originMoney) {
        _originMoney = ({
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(frameWith* 0.5,CGRectGetMaxY(self.self.YieldLabel.frame)  + frameHeight * 0.01,frameWith* 0.5 , frameHeight * 0.07)];
            label.textAlignment = NSTextAlignmentLeft;
            label.font = [UIFont systemFontOfSize:20.0];
            label;
        });

    }
    return _originMoney;
}

/**
 *  发售银行
 *
 *  @return
 */
- (UIImageView *)sellBank{
    if (!_sellBank) {
        _sellBank = [[UIImageView alloc]initWithFrame:CGRectMake(frameWith* 0.55,CGRectGetMaxY(self.self.originMoney.frame)  + frameHeight * 0.02,frameWith* 0.4 , frameHeight * 0.06)];
        _sellBank.image = [UIImage imageNamed:@"理财_苏州银行"];
    }
    return _sellBank;
}
/**
 *  认购起始日
 *
 *  @return
 */
- (UILabel *)buyOriginDay{
    if (!_buyOriginDay) {
        _buyOriginDay = ({
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(frameWith* 0.38,CGRectGetMaxY(self.self.sellBank.frame)  + frameHeight * 0.055,frameWith* 0.52 , frameHeight * 0.07)];
            label.textAlignment = NSTextAlignmentLeft;
            
            
            label;
        });

    }
    return _buyOriginDay;
}
/**
 *  认购结束日
 *
 *  @return
 */
- (UILabel *)buyDeadDay{
    if (!_buyDeadDay) {
        _buyDeadDay = ({
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(frameWith* 0.38,CGRectGetMaxY(self.self.buyOriginDay.frame)  ,frameWith* 0.52 , frameHeight * 0.07)];
            label.textAlignment = NSTextAlignmentLeft;
            
            
            label;
        });

    }
    return _buyDeadDay;
}
/**
 *  起息日
 *
 *  @return
 */
- (UILabel *)originInterest{
    if (!_originInterest) {
        _originInterest = ({
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(frameWith* 0.38,CGRectGetMaxY(self.self.buyDeadDay.frame)  ,frameWith* 0.52 , frameHeight * 0.07)];
            label.textAlignment = NSTextAlignmentLeft;
          
            
            label;
        });

    }
    return _originInterest;
}
/**
 *  到期日
 *
 *  @return
 */
- (UILabel *)deadInterest{
    if (!_deadInterest) {
        _deadInterest = ({
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(frameWith* 0.38,CGRectGetMaxY(self.self.originInterest.frame) ,frameWith* 0.52 , frameHeight * 0.07)];
            label.textAlignment = NSTextAlignmentLeft;
            label;
        });

    }
    return _deadInterest;
}
/**
 *  收益试算
 */

- (UIButton *)incomeCalculation{
    if (!_incomeCalculation) {
        _incomeCalculation = [[UIButton alloc]initWithFrame:CGRectMake(frameWith* 0.2,CGRectGetMaxY(self.deadInterest.frame) + frameHeight * 0.04 ,frameWith* 0.3 , frameHeight * 0.1)];
        [_incomeCalculation setBackgroundImage:[LocalDataManager getImageWithSourceOfPath:@"理财_收益试算"] forState:UIControlStateNormal];
        [_incomeCalculation addTarget:self action:@selector(action_button:) forControlEvents:UIControlEventTouchUpInside];
        _incomeCalculation.tag = 100;
    }
    return _incomeCalculation;
}
- (UIButton *)addToCompare{
    if (!_addToCompare) {
        _addToCompare = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.bounds) - frameWith * 0.38,CGRectGetMaxY(self.self.deadInterest.frame) + frameHeight * 0.04 ,frameWith* 0.3 , frameHeight * 0.1)];
        [_addToCompare setBackgroundImage:[LocalDataManager getImageWithSourceOfPath:@"理财_加入对比"] forState:UIControlStateNormal];
        [_addToCompare addTarget:self action:@selector(action_button:) forControlEvents:UIControlEventTouchUpInside];
        _addToCompare.tag = 101;
    }
    return _addToCompare;
}
@end
